package com.moodswings.server;

import com.moodswings.server.model.Mood;
import com.moodswings.server.service.MoodService;
import flexjson.JSONSerializer;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;
import java.util.List;

/**
 * prihvata zahtev, sakupi poslednjih 10 unetih podataka i vrati ih kao JSON objekat
 */
public class MapServlet extends HttpServlet {

	MoodService moodService;
	JSONSerializer serializer = new JSONSerializer();

	public MapServlet() {
		moodService = new MoodService();

	}

	/**
	 * Pokupi zadnje unose, spakuje ih u json objekat i posalje kao odgovor na zahtev
	 */
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		List<Mood> moodList = moodService.findLastMoods(10);      //pokupi poslednjih 10 unosa u bazu podataka

		//vrati ih kao odgovor na zahtev
		Writer writer = resp.getWriter();
		String json = serializer.exclude("class").serialize(moodList);
		writer.write(json);
	}
}
